package com.leruan.aasimp.action.newcar;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.leruan.aasimp.bean.NewCarSaleBean;
import com.leruan.aasimp.bean.SaleCarTypeBean;
import com.leruan.aasimp.constant.UsedCarConstant;
import com.leruan.aasimp.entity.CarMainInfo;
import com.leruan.aasimp.entity.Eauser;
import com.leruan.aasimp.service.BasicCarService;
import com.leruan.aasimp.service.EauserService;
import com.leruan.aasimp.service.NewCarService;
import com.leruan.aasimp.service.VendorService;
import com.leruan.aasimp.util.SecurityUtils;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.org.apache.regexp.internal.recompile;

public class NewCarAction extends ActionSupport{

	private static final long serialVersionUID = -418779806132743378L;
	
	@Resource
	private NewCarService newcarService;
	@Resource
	private EauserService eauserService;
	@Resource
	private BasicCarService basicCarService;
	@Resource
	private VendorService vendorService;
	
	public String showSale(){
		
		Map<String, String> map = new HashMap<String, String>();
		
 		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		
		if("0".equals(request.getParameter("isQuery")))
			session.removeAttribute("map");
		else if(session.getAttribute("map") != null)
			map = (Map<String, String>) session.getAttribute("map");
		
		int pageNo = 1;
		if(request.getParameter("no") != null)
			pageNo = Integer.parseInt(request.getParameter("no"));
		String vin = request.getParameter("vin");
		request.setAttribute("vin", vin);
		try {
			List<NewCarSaleBean> newCarList =  newcarService.getNewCarList(map, pageNo);
			
			List<NewCarSaleBean> newCarList1 = new ArrayList<NewCarSaleBean>();
			for(NewCarSaleBean nBean : newCarList){
				nBean.setCarOwnerName(nBean.getCarOwnerName().substring(0, 2) + "*");
				nBean.setIdentifcationNum(nBean.getIdentifcationNum().substring(0, 14)+"****");
				nBean.setPlateNum(nBean.getPlateNum().substring(0, 6)+ "**");
				newCarList1.add(nBean);
			}
			Long total = newcarService.getNewCarTotal(map);
			
			request.setAttribute("newCarList", newCarList1);
			session.setAttribute("pageNoCar", pageNo);
			Long totalNo = total / UsedCarConstant.PAGE_SIZE_3 + 1;
			session.setAttribute("totalNoCar", totalNo);
			session.setAttribute("totalCar", total);
			if (pageNo < 3 || totalNo < 5) {
				session.setAttribute("pageNoCenterCar", 3);
			} else if (pageNo > totalNo - 2) {
				session.setAttribute("pageNoCenterCar", totalNo - 2);
			} else {
				session.setAttribute("pageNoCenterCar", pageNo);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "showSale";
	}
	
	public String logout() {
		ActionContext context = ServletActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest) context
				.get(ServletActionContext.HTTP_REQUEST);
		request.getSession().removeAttribute("eauser");
		return "index";
	}
	
	public String login() {
		ActionContext context = ServletActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest) context
				.get(ServletActionContext.HTTP_REQUEST);
		String username = request.getParameter("username").trim();
		String password = request.getParameter("password");

		Eauser eauser = eauserService.getEauserByUn(username);
		if (eauser == null) {
			request.setAttribute("message", "用户名不存在！");
			return "login";
		}
		String pwd = SecurityUtils.encodeByMD5(password);
			
		if (eauser.getPassword().equals(pwd)) {
			request.getSession().setAttribute("eauser", eauser);
		} else {
			request.setAttribute("message", "密码输入有误！");
		}
		return "login";
	}
	
	public String showCarType(){
		HttpServletRequest request = ServletActionContext.getRequest();
		
		try {
			List<String> brandList = basicCarService.getBrandList();
			request.setAttribute("brands", brandList);
			
			Date date = new Date();
			StringBuffer startTime = new StringBuffer(date.getYear()+1900+"");
			if(date.getMonth() < 9)
				startTime.append("-0" + (date.getMonth()+1));
			else
				startTime.append("-" + (date.getMonth()+1));
			if(date.getDate() < 10)
				startTime.append("-0" + date.getDate());
			else
				startTime.append("-" + date.getDate());
			request.setAttribute("startTime", startTime);
			request.setAttribute("endTime", startTime);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "showCarType";
	}
	
	
	public String CarTypeQuery(){
		
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			String startTime = request.getParameter("startTime");
			String endTime = request.getParameter("endTime");
			String brand = request.getParameter("brand");
			String department = request.getParameter("department");
			String carType = request.getParameter("carType");
			
			List<String> brandList = basicCarService.getBrandList();
			request.setAttribute("brands", brandList);
			request.setAttribute("startTime", startTime);
			request.setAttribute("endTime", endTime);
			request.setAttribute("brand", brand);
			request.setAttribute("department", department);
			request.setAttribute("carType", carType);
			
			Map <String, String> map = new HashMap<String, String>();
			map.put("startTime", startTime);
			map.put("endTime", endTime);
			if(brand != null && !"0".equals(brand.trim()))
				map.put("brand", brand);
			if(department != null && !"0".equals(department.trim()))
				map.put("department", department);
			if(carType != null && !"0".equals(carType.trim()))
				map.put("carType", carType);
			List<SaleCarTypeBean> sList = newcarService.getCarTypePh(map);
			request.setAttribute("sList", sList);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "showCarType";
	}
	
	public String showVendor(){
		HttpServletRequest request = ServletActionContext.getRequest();
		
		try {
			List<String> vendorList = vendorService.getVendorList();
			request.setAttribute("vendors", vendorList);
			
			Date date = new Date();
			StringBuffer startTime = new StringBuffer(date.getYear()+1900+"");
			if(date.getMonth() < 9)
				startTime.append("-0" + (date.getMonth()+1));
			else
				startTime.append("-" + (date.getMonth()+1));
			if(date.getDate() < 10)
				startTime.append("-0" + date.getDate());
			else
				startTime.append("-" + date.getDate());
			request.setAttribute("startTime", startTime);
			request.setAttribute("endTime", startTime);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "showVendor";
	}
	
	public String VendorQuery(){
		HttpServletRequest request = ServletActionContext.getRequest();
		
		request.getSession().removeAttribute("map");
		
		try {
			String startTime = request.getParameter("startTime");
			String endTime = request.getParameter("endTime");
			String vendorName = request.getParameter("vendorName");
			
			request.setAttribute("startTime", startTime);
			request.setAttribute("endTime", endTime);
			
			List<String> vendorList = vendorService.getVendorList();
			request.setAttribute("vendors", vendorList);
			
			Map <String, String> map = new HashMap<String, String>();
			map.put("startTime", startTime);
			map.put("endTime", endTime);
			if(vendorName != null && !"0".equals(vendorName.trim()))
				map.put("vendorName", vendorName);
			List<SaleCarTypeBean> sList = vendorService.getVendor(map);
			request.setAttribute("sList", sList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "showVendor";
	}
	
	public String newCarSaleQueryShow(){
		
		HttpServletRequest request = ServletActionContext.getRequest();
		request.getSession().removeAttribute("map");
		
		try {
			List<String> brandList = basicCarService.getBrandList();
			request.setAttribute("brands", brandList);
			
			Date date = new Date();
			StringBuffer startTime = new StringBuffer(date.getYear()+1900+"");
			if(date.getMonth() < 9)
				startTime.append("-0" + (date.getMonth()+1));
			else
				startTime.append("-" + (date.getMonth()+1));
			if(date.getDate() < 10)
				startTime.append("-0" + date.getDate());
			else
				startTime.append("-" + date.getDate());
			request.setAttribute("startTime", startTime);
			request.setAttribute("endTime", startTime);
			
			List<String> vendorList = vendorService.getVendorList();
			request.setAttribute("vendors", vendorList);
			
			request.setAttribute("priceList", UsedCarConstant.priceList);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "newCarSaleQueryShow";
	}
	
	public String newCarSaleQuery(){
		
		HttpServletRequest request = ServletActionContext.getRequest();
		String brand = request.getParameter("brand");
		String department = request.getParameter("department");
		String carType = request.getParameter("carType");
		String vin = request.getParameter("vin");
		String carOwnerName = request.getParameter("carOwnerName");
		String identifcationNum = request.getParameter("identifcationNum");
		String plateNum = request.getParameter("plateNum");
		String color = request.getParameter("color");
		String vendorName = request.getParameter("vendorName");
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		String money = request.getParameter("money");
		
		Map<String, Object> map = new HashMap<String, Object>();
		if(brand != null & !"0".equals(brand.trim()))
			map.put("brand", brand);
		if(department != null & !"0".equals(department.trim()))
			map.put("department", department);
		if(carType != null & !"0".equals(carType.trim()))
			map.put("carType", carType);
		if(vin != null & !"".equals(vin.trim()))
			map.put("vin", vin);
		if(carOwnerName != null & !"".equals(carOwnerName.trim()))
			map.put("carOwnerName", carOwnerName);
		if(identifcationNum != null & !"".equals(identifcationNum.trim()))
			map.put("identifcationNum", identifcationNum);
		if(plateNum != null & !"".equals(plateNum.trim()))
			map.put("plateNum", plateNum);
		if(color != null & !"0".equals(color.trim()))
			map.put("color", color);
		if(vendorName != null & !"0".equals(vendorName.trim()))
			map.put("vendorName", vendorName);
		if(startTime != null & !"".equals(startTime.trim()))
			map.put("startTime", startTime);
		if(endTime != null & !"".equals(endTime.trim()))
			map.put("endTime", endTime);
		if(money != null & !"0".equals(money.trim()))
			map = UsedCarConstant.convertPrice(map, money);
		request.getSession().setAttribute("map", map);
		
		return showSale();
	}
	
	public void setNewcarService(NewCarService newcarService) {
		this.newcarService = newcarService;
	}

	public void setEauserService(EauserService eauserService) {
		this.eauserService = eauserService;
	}

	public void setBasicCarService(BasicCarService basicCarService) {
		this.basicCarService = basicCarService;
	}

	public void setVendorService(VendorService vendorService) {
		this.vendorService = vendorService;
	}
	
}
